<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */

namespace app\api\lib;

use app\common\controller\Base;
use app\common\lib\GlobalConfig;

class Pay extends Base
{

    private $return_url = "";

    /**
     * 支付统一接口
     * @param string $order_sn 订单号
     * @param string $pay_type 支付方式
     * @param string $openid openid 非必填
     * @return bool|void
     */
    public function pay($order_sn = '', $pay_type = GlobalConfig::WX_PAY, $openid = ''){
        //TODO 处理商品数据

        //此为传递给支付的参数
        $payData = array(
            'body' => '商品名称',
            'subject' => '2020061215305986', //订单号
            'total_amount' => '0.01', //分
            'out_trade_no' => '20200612153045', //支付单号
            'back_params' => array() //回传参数
        );

        //TODO 去支付
        switch ($pay_type){
            case GlobalConfig::ALIPAY_PC_PAY:
                return $this->alipayPcPay($payData);
                break;
            case GlobalConfig::ALIPAY_WAP_PAY:
                return $this->alipayWapPay($payData);
                break;
            case GlobalConfig::ALIPAY_APP_PAY:
                return $this->alipayAppPay($payData);
                break;
            case GlobalConfig::WX_PAY:
                return $this->wxPay($payData);
                break;
            case GlobalConfig::WX_QR_PAY:
                return $this->wxQrPay($payData);
                break;
            case GlobalConfig::WX_APP_PAY:
                return $this->wxAppPay($payData);
                break;
            case GlobalConfig::WX_MINA_PAY:
                return $this->wxMinaPay($payData,$openid);
                break;
            default:
                return false;
        }

    }

    //支付方式
    private function getPayType($pay_type){
        $code = 0;
        switch($pay_type){
            case GlobalConfig::ALIPAY_PC_PAY:
                $code = 0;
                break;
            case GlobalConfig::ALIPAY_WAP_PAY:
                $code = 0;
                break;
            case GlobalConfig::ALIPAY_APP_PAY:
                $code = 0;
                break;
            case GlobalConfig::WX_PAY:
                $code = 1;
                break;
            case GlobalConfig::WX_QR_PAY:
                $code = 1;
                break;
            case GlobalConfig::WX_APP_PAY:
                $code = 1;
                break;
            case GlobalConfig::WX_MINA_PAY:
                $code = 1;
                break;
            default:
                break;
        }
        return $code;
    }

    /**
     * 支付宝电脑端支付
     * @param array $payData
     */
    private function alipayPcPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/alipay/alipay.php';
        $alipay_config = config(GlobalConfig::ALIPAY_PAY);
        $return_url = $this->return_url;
        $alipay = new \alipay($alipay_config);
        $response = $alipay->pcPay($payData,$return_url);
        if($response){
            //成功无返回，自动跳转到return_url

        }else{
            halt('支付失败');
        }

    }

    /**
     * 支付宝wap端支付
     * @param array $payData
     */
    private function alipayWapPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/alipay/alipay.php';
        $alipay_config = config(GlobalConfig::ALIPAY_PAY);
        $return_url = $this->return_url;
        $alipay = new \alipay($alipay_config);
        $response = $alipay->wapPay($payData,$return_url);
        if($response){
            //成功无返回，自动跳转到return_url
        }else{
            halt('支付失败');
        }
    }

    /**
     * 支付宝APP端支付
     * @param array $payData
     */
    private function alipayAppPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/alipay/alipay.php';
        $alipay_config = config(GlobalConfig::ALIPAY_PAY);
        $alipay = new \alipay($alipay_config);
        $response = $alipay->appPay($payData);
        if($response){

            //成功返回给APP客户端的参数
        }else{
            halt('支付失败');
        }
    }

    /**
     * 支付宝异步回调
     */
    public function alipayNotifyVerify(){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/alipay/alipay.php';
        $alipay_config = config(GlobalConfig::ALIPAY_PAY);
        $alipay = new \alipay($alipay_config);
        $result = $alipay->notifyVerify();
        if($result){
            //商户订单号(支付号)
            $out_trade_no = $_POST['out_trade_no'];
            //支付宝交易号
            $trade_no = $_POST['trade_no'];
            //回传参数
            $back_params = $_POST['biz_content'];

            //todo 处理支付成功后的信息
            $this->paySuccessHandle($out_trade_no,$trade_no,$back_params);

            echo "success"; //必须
        }else{
            echo "fail";  //勿修改
        }
    }

    /**
     * 微信公众号支付
     * @param array $payData
     */
    private function wxPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/wxpay/wxpay.php';
        $wxpay_config = config(GlobalConfig::WECHAT_OFFICIAL_ACCOUNT_PAY);
        $wxpay = new \wxpay($wxpay_config);
        $response = $wxpay->wxPay($payData);
        if($response){
            //成功返回前端支付参数

        }else{
            halt('支付失败');
        }
    }

    /**
     * 微信APP支付
     * @param array $payData
     */
    private function wxAppPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/wxpay/wxpay.php';
        $wxpay_config = config(GlobalConfig::WECHAT_APP_PAY);
        $wxpay = new \wxpay($wxpay_config);
        $response = $wxpay->appPay($payData);
        if($response){
            //成功返回APP参数
        }else{
            halt('支付失败');
        }
    }

    /**
     * 微信小程序支付
     * @param array $payData
     */
    private function wxMinaPay($payData = array(),$openid){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/wxpay/wxpay.php';
        $wxpay_config = config(GlobalConfig::WECHAT_MINA_PAY);
        $wxpay = new \wxpay($wxpay_config);
        $response = $wxpay->wxMinaPay($payData,$openid);
        halt($response);
        if($response){
            //成功返回APP参数
            return $response;
        }else{
            return false;
        }
    }

    /**
     * 微信扫码支付
     * @param array $payData
     */
    private function wxQrPay($payData = array()){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/wxpay/wxpay.php';
        $wxpay_config = config(GlobalConfig::WECHAT_APP_PAY);
        $wxpay = new \wxpay($wxpay_config);
        $code_url = $wxpay->qrPay($payData);
        if($code_url){
            //成功返回支付的扫码URL
        }else{
            halt('支付失败');
        }
    }

    /**
     * 生成微信扫码二维码
     */
    public function wxQrcode()
    {
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/phpqrcode/phpqrcode.php';
        $url = urldecode($_GET['data']);
        \QRcode::png($url,false,'H',10);
    }

    /**
     * 微信异步通知
     */
    public function wxpayNotifyVerify($wxpay_config){
        require_once $_SERVER['DOCUMENT_ROOT'] . '/../extend/payment/wxpay/wxpay.php';
        $wxpay = new \wxpay($wxpay_config);
        $result = $wxpay->notifyVerify();
        if($result == false){
            return;
        }
        //订单号(支付号)
        $out_trade_no = $result['out_trade_no'];
        //支付宝交易号
        $trade_no = $result['transaction_id'];
        //回传参数
        $back_params = $result['attach'];

        //todo 处理支付成功后的信息
        $this->paySuccessHandle($out_trade_no,$trade_no,$back_params);

        //回传微信必须
        $back_wx = array('return_code' => 'SUCCESS', 'return_msg' => 'OK');
        echo toXml($back_wx);
    }

    //支付成功后处理数据
    private function paySuccessHandle($pay_sn,$trade_code,$back_params = array()){

        return true;
    }

}